package com.toocms.jinjuhang.config

import com.blankj.utilcode.util.SPUtils
import com.toocms.tab.expand.data.LoginStatusRepository
import com.toocms.jinjuhang.data.User

object UserRepository : LoginStatusRepository() {
    var user = User()
        private set
    private val spUtils: SPUtils = SPUtils.getInstance(Constants.Extra.SP_NAME_USERDATA)

    init {
        readUser()
    }

    fun updateUser(user: User) {
        this.user = user
        var javaClass = UserRepository.user.javaClass
        javaClass.declaredFields.forEach {
            it.isAccessible = true
            spUtils.put(it.name, it.get(user).toString())
        }
    }

    private fun readUser() {
        var javaClass = user.javaClass
        javaClass.declaredFields.forEach {
            it.isAccessible = true
            it.set(user, spUtils.getString(it.name, ""))
        }

    }

}